package com.hbzhit.modules.act.handle.impl;

import com.hbzhit.modules.act.factory.ProcessServiceFactory;
import com.hbzhit.modules.act.handle.WorkFlowQuery;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.history.HistoricActivityInstanceQuery;
import org.activiti.engine.history.HistoricProcessInstanceQuery;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.runtime.ProcessInstanceQuery;
import org.activiti.engine.task.Task;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 流程处理查询类
 *
 * @author DING
 * @date 2019年7月23日
 */
@Slf4j
@Component("workFlowQueryHandler")
public class WorkFlowQueryHandler extends ProcessServiceFactory implements WorkFlowQuery {

    @Override
    public ProcessInstanceQuery createProcessInstanceQuery() {
        return runtimeService.createProcessInstanceQuery();
    }

    @Override
    public HistoricProcessInstanceQuery createHistoricProcessInstanceQuery() {
        return historyService.createHistoricProcessInstanceQuery();
    }

    @Override
    public HistoricActivityInstanceQuery createHistoricActivityInstanceQuery() {
        return historyService.createHistoricActivityInstanceQuery();
    }

    @Override
    public List<String> getActiveActivityIds(String processInstanceId) {
        return runtimeService.getActiveActivityIds(processInstanceId);
    }

    @Override
    public ProcessDefinition getProcessDefinition(String processDefinitionId) {
        return repositoryService.getProcessDefinition(processDefinitionId);
    }

    @Override
    public ProcessInstance processInstanceId(String processInstanceId) {
        return createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
    }

    @Override
    public ProcessInstance processInstanceBusinessKey(String processInstanceBusinessKey) {
        return createProcessInstanceQuery().processInstanceBusinessKey(processInstanceBusinessKey).singleResult();
    }

    @Override
    public boolean isProcessfinished(String processInstanceId) {
        ProcessInstance pi = processInstanceId(processInstanceId);

        return pi == null;
    }

    @Override
    public ProcessInstance taskId(String taskId) {
        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();

        String processInstId = task.getProcessInstanceId();

        return this.processInstanceId(processInstId);
    }

}

